package modes

import (
	"fmt"
	"time"
)

/*
 * 描述: 科目表
 *
 * start 状态  0 启用  1 禁用
 *
 *****************************/

type Subject struct {
	Id    int64  `json:"s_id" xorm:"id"`       //科目ID
	PId   int64  `json:"s_pid" xorm:"pid"`     //项目ID
	Name  string `json:"s_name" xorm:"name"`   //科目名称
	At    int64  `json:"s_at" xorm:"at"`       //时间
	Start int    `json:"s_start" xorm:"start"` //状态  0 启用  1 禁用
	Year  int    `json:"s_year" xorm:"year"`   //年份
}

type JoinCourse struct {
	SubClass `xorm:"extends"`
	Subject  `xorm:"extends"`
	KId      int64 `json:"k_id" xorm:"-"`
}

func (this *Subject) TableName() string {
	return "subject"
}

func (this *Subject) Save() (int64, error) {
	this.At = time.Now().Unix()
	return Db(0).Insert(this)
}

func (this *Subject) Get() (bool, error) {
	var val Subject
	var where string

	if (this.Id%100000) == 0 && this.Id > 0 {
		where = fmt.Sprintf("id > %d and id < %d and pid = %d and name = '%s' and year = %d ", this.Id, this.Id+10000-1, this.PId, this.Name, this.Year)
	} else if (this.Id%1000) == 0 && this.Id > 0 {
		where = fmt.Sprintf("id > %d and id < %d and pid = %d and name = '%s' and year = %d ", this.Id, this.Id+1000-1, this.PId, this.Name, this.Year)
	} else {
		where = fmt.Sprintf(" pid = %d and name = '%s' ", this.PId, this.Name)
	}
	return Db(0).Where(where).Get(&val)
}

func (this *Subject) IdDel() (int64, error) {
	where := fmt.Sprintf("id = %d", this.Id)
	return Db(0).Where(where).Delete(this)
}

func (this *Subject) update(where string, field string) (int64, error) {
	return Db(0).Where(where).Cols(field).Update(this)
}

func (this *Subject) IdSet(field string) (int64, error) {
	where := fmt.Sprintf(" id = %d  ", this.Id)
	return this.update(where, field)
}

func (this *Subject) Count(where string) (int64, error) {
	var val Subject
	return Db(0).Where(where).Count(&val)
}
func (this *Subject) GetList() ([]Subject, error) {
	list := make([]Subject, 0)
	err := Db(0).Where(" id%100000=0").Find(&list)
	return list, err
}

func (this *Subject) where(fage, count, page int, where, field string) ([]Subject, error) {
	list := make([]Subject, 0)
	var err error
	page--
	if 0 == fage { // 从小到大排序
		err = Db(0).Where(where).
			Asc(field).
			Limit(count, page*count).
			Find(&list)
	} else { // 从大到小排序
		err = Db(0).Where(where).
			Desc(field).
			Limit(count, page*count).
			Find(&list)
	}
	return list, err
}

type JoinProject struct {
	Id       int64 `json:"s_id" xorm:"id"` //科目ID
	Subject  `xorm:"extends"`
	Project  `xorm:"extends"`
	Resource `xorm:"extends"`
}

func (this *JoinProject) KcIdList(val *Pager) error {
	list := make([]JoinProject, 0)
	val.TName = "subject"
	val.Page--
	if val.Page <= 0 {
		val.Page = 0
	}
	var field, sort, paixu string
	if this.Id == 0 {
		field = fmt.Sprintf("%s.id %% 100000 = 0 and project.start = 0 and resources.s_type = 0 and resources.start = 0 and %s.start = 0", val.TName, val.TName)
	} else if (this.Id % 10000) == 0 {
		// field = fmt.Sprintf("%s.id > %d and %s.id < %d and %s.id %% 100 = 0 and project.start = 0 and resources.s_type = 0 and resources.start = 0 and %s.start = 0 ", val.TName, this.Id, val.TName, this.Id+10000, val.TName, val.TName)
		field = fmt.Sprintf("%s.id > %d and %s.id < %d  and project.start = 0 and resources.s_type = 0 and resources.start = 0 and %s.start = 0 ", val.TName, this.Id, val.TName, this.Id+1000, val.TName)
	} else {
		field = fmt.Sprintf("%s.id > %d and %s.id < %d and project.start = 0 and resources.s_type = 0 and resources.start = 0 and %s.start = 0", val.TName, this.Id, val.TName, this.Id+1000, val.TName)
	}
	val.Total, _ = Db(1).Table(val.TName).
		Join("INNER", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		Join("INNER", "resources", fmt.Sprintf("%s.id = resources.sid", val.TName)).
		Where(field).
		Where(val.ToWhere()).
		Count()

	paixu = fmt.Sprintf("%s.id", val.TName) // 默认ID

	if 1 == val.Rules {
		sort = fmt.Sprintf("%s desc", paixu)
	} else {
		sort = fmt.Sprintf("%s asc", paixu)

	}
	err := Db(1).Table(val.TName).
		Join("LEFT", "project", fmt.Sprintf("%s.pid = project.id", val.TName)).
		Join("LEFT", "resources", fmt.Sprintf("%s.id = resources.sid", val.TName)).
		Where(field).
		Where(val.ToWhere()).
		OrderBy(sort).
		Limit(val.Count, val.Page*val.Count).
		Find(&list)
	if err == nil {
		val.List = list
	}
	return err
}

func (this *Subject) GetName(kid []int64) ([]Subject, error) {
	list := make([]Subject, 0)
	err := Db(1).Where("start = 0").In("id", kid).Find(&list)
	return list, err
}

func (this *TSubject) GetAllSubject() ([]TSubject, error) {
	subjListAll := make([]TSubject, 0)
	err := Db(0).Table("subject").Where("start = 0").Find(&subjListAll)
	return subjListAll, err
}

//where := fmt.Sprintf(" uid=%d and sid>%d and sid<%d ", this.UId, cId, (cId + 100000))

func (this *Subject) GetAllKcList(cmid int64) ([]Subject, error) {
	subjListAll := make([]Subject, 0)
	err := Db(0).Table("subject").Where(fmt.Sprintf("start = 0  and id> %d and id< %d  and id %% 1000=0", cmid, (cmid + 100000))).Find(&subjListAll)
	return subjListAll, err
}
